package util

import java.io.{ObjectInputStream, ByteArrayInputStream, ObjectOutputStream, ByteArrayOutputStream}

/**
 * <p>
 * Created by IntelliJ IDEA.
 * User: zslajchrt
 * Date: Feb 10, 2011
 * Time: 8:18:03 PM
 */

object SerializationUtils {

  def serialize[T <: AnyRef](obj: T): Array[Byte] = {
    val bos = new ByteArrayOutputStream()
    val oos = new ObjectOutputStream(bos)
    oos.writeObject(obj)
    oos.close
    bos.toByteArray
  }

  def deserialize[T](data: Array[Byte]): T = {
    val bis = new ByteArrayInputStream(data)
    val ois = new ObjectInputStream(bis)
    ois.readObject().asInstanceOf[T]
  }

}